import { createRouter, createMemoryHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";

const Home = () => import("../views/Home/Home.vue")
const EmployeeManagement = () => import("../views/EmployeeManagement/EmployeeManagement.vue")
const Attendance = () => import("../views/Attendance/Attendance.vue")
const WorkMate = () => import("../views/WorkMate/WorkMate.vue")
const Welcome = () => import("../views/Welcome/Welcome.vue")
const LeaveManagement = () => import("../views/LeaveManagement/LeaveManagement.vue")
const PayrollManagement = () => import("../views/PayrollManagement/PayrollManagement.vue")

const routes: RouteRecordRaw[] = [
    {
        path: "/welcome",
        component: Welcome
    }, {
        path: "/",
        component: Home,
        meta: { requiresAuth: true }, //给需要验证的网页添加一个meta属性来让路由守卫判断是否需要验证
        children: [
            {
                path: "employee",
                component: EmployeeManagement
            },
            {
                path: "workMate",
                component: WorkMate
            },
            {
                path: "attendance",
                component: Attendance
            },
            {
                path: "leave",
                component: LeaveManagement
            },
            {
                path: "payroll",
                component: PayrollManagement
            }
        ]
    }

]

const router = createRouter({
    history: createMemoryHistory(import.meta.env.BASE_URL),
    routes
})

// import { getData } from "../api/api" //导入封装好的axios请求
// const verifyToken = async () => { //定义一个方法用来查询cookie里是否有token
//     const res = await getData("/verify-token") //发送请求给后端验证cookie里是否有token
//
//     if (res.authenticated) {  //如果后端返回true则说明存在token
//         return true
//     } else {
//         return false //反之后端返回false则说明不存在token
//     }
// }

//路由守卫
// router.beforeEach(async (to, from, next) => {
//     const haveToken = await verifyToken() //调用验证token的方法
//
//     //通过提前设置的meta属性来判断页面是否需要验证，以及是否存在token
//     if (to.meta.requiresAuth && !haveToken) {
//         next("/welcome") //跳转回登录页
//     } else {
//         next() // 别忘了添加这行，否则路由不会继续
//     }
// })

export default router
